const Koa = require('koa');
const app = new Koa();
const mongoose = require('mongoose')
const {connect,initSchemas} = require('./database/init.js')
const Router = require("koa-router")
let user = require("./appAPI/User.js")
const bodyParser = require('koa-bodyparser')
const cors = require("koa-cors")
app.use(cors())
app.use(bodyParser())
// 装载所有的子路由
let router = new Router()
router.use('/user',user.routes())
// 加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())
// 立即执行函数
;(async()=>{
    await connect()
    initSchemas()
    const User = mongoose.model('User')
    // let oneUser = new User({userName:'zhangsan4',password:"1234567"})
    // oneUser.save().then(()=>{
    //     console.log('插入成功')
    // })
    let users = await User.findOne({}).exec()
    console.log("----------------------------")
    console.log(users)
    console.log("----------------------------")
})()

app.listen(3000,()=>{
    console.log("port 3000")
})
